import numpy as np
import matplotlib.pyplot as plt

X1 = np.random.rand(100, 1)
X2 = np.random.rand(100, 1)
# 查看方法注释
print(help(np.random.rand))
print('X'.center(50, '-'))
print(X1)
Y = 5 + 4 * X1 + 3 * np.power(X2, 2) + np.random.rand(100, 1)
print('Y'.center(50, '-'))
print(Y)
X_b = np.c_[np.ones((100, 1)), X1, X2]
print('X_b'.center(50, '-'))
print(X_b)

# 这里的.T 就是 transpose 转置的作用，dot 就是向量点乘， np.linalg.inv 是矩阵求逆
# (XT *X )求逆 * XT * Y
theta_best = np.linalg.inv(X_b.T.dot(X_b)).dot(X_b.T).dot(Y)
print('theta_best'.center(50, '-'))
print(theta_best)

# X_new = np.array([[0], [2]])
# X_new_b = np.c_[(np.ones((2, 1))), X_new]
# # print(X_new_b)
# y_predict = X_new_b.dot(theta_best)
# plt.plot(X_new, y_predict, 'r-')
# plt.plot(X1, Y, 'b.')
# plt.axis([0, 2, 0, 15])
# plt.show()
